package net.jxvtc.blogv1.service;

import net.jxvtc.blogv1.domain.Authority;
import net.jxvtc.blogv1.domain.User;
import net.jxvtc.blogv1.repository.AuthorityRepository;
import net.jxvtc.blogv1.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Optional;

/**
 * @program: blogv1
 * @description: 用户服务接口实现类
 * @author: 蓝建平
 * @create: 2020-10-24 10:48
 **/
@Service
public class UserServiceImpl implements UserService  {


    @Autowired
    private UserRepository userRepository;



    /**
     * 新增、编辑、保存用户 （后台管理员操作)
     *
     * @param user
     * @return
     */
    @Transactional
    @Override
    public User saveOrUpateUser(User user) {
        user.setPassword(new BCryptPasswordEncoder().encode(user.getPassword()));
        User user1 = userRepository.save(user);

        return user1;
    }

    /**
     * 注册用户  （前台用户）
     *
     * @param user
     * @return
     */
    @Transactional
    @Override
    public User registerUser(User user) {
        user.setPassword(new BCryptPasswordEncoder().encode(user.getPassword()));
        User user1 = userRepository.save(user);

        return user1;
    }

    /**
     * 删除用户  （后台管理员操作)
     *
     * @param id
     */
    @Transactional
    @Override
    public void removeUser(Long id) {
        Optional<User> user = userRepository.findById(id);
        userRepository.deleteById(id);


    }

    /**
     * 根据id获取用户   （前后台）
     *
     * @param id
     * @return
     */
    @Override
    public User getUserById(Long id) {
        Optional<User> optional = userRepository.findById(id);
        if(optional.isPresent())
        {
            return optional.get();
        }
        return null;
    }

    /**
     * 根据用户名进行分页模糊查询  （后台管理员操作)
     *
     * @param name
     * @param pageable
     * @return
     */
    @Override
    public Page<User> listUsersByNameLike(String name, Pageable pageable) {
        // 模糊查询
        name = "%" + name + "%";
        return
              userRepository.findByNameLike(name,pageable);

    }



    @Autowired
    private AuthorityRepository authorityRepository;





    @Override
    public User getUser(String username) {
        User user=null;

            user = userRepository.findByUsername(username);

        return user;
    }

    @Override
    public List<Authority> getUserAuthority(String username) {
        List<Authority> authorities=null;

            authorities = authorityRepository.findAuthoritiesByUsername(
                    username);

        return authorities;
    }


}
